package com.weixin.common.sftp;

import com.jcraft.jsch.SftpException;
//import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * service
 *
 * @author zh
 */
@Service
public class SftpTest {

    @Resource
    private SftpTemplate sftpTemplate;

    public void multiHost(String hostName, String path) {
        HostHolder.changeHost(hostName);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            InputStream inputStream = sftpTemplate.download(path);
            IOUtils.copy(inputStream, byteArrayOutputStream);
        } catch (IOException | SftpException e) {
            e.printStackTrace();
        } finally {
            sftpTemplate.returnSftpClient();
        }
        System.out.println(byteArrayOutputStream.size());
    }

    public void multiHost2(String hostName, String path) {
        HostHolder.changeHost(hostName);
        try {
            sftpTemplate.download(path, "E:\\upload\\" + hostName);
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
}
